//
//  OwnViewController.m
//  project_delicious
//
//  Created by 章历杰 on 15/3/21.
//  Copyright (c) 2015年 lanou3g.com 蓝鸥科技. All rights reserved.
//

#import "OwnViewController.h"
#import "MineSnap.h"
#import "UIColor+AddColor.h"
#import "MineFavorTableViewController.h"

#import "OponionViewController.h"

@interface OwnViewController ()<UIScrollViewDelegate, UIAlertViewDelegate>

//页面是一个ScrollView
//@property (nonatomic, retain)UIScrollView *scrollView;
@property (nonatomic, retain)UIAlertView *alertView;
@property (nonatomic, retain)NSString *fileCount;
@end

@implementation OwnViewController
- (void)dealloc
{
    [_fileCount release];
    [_alertView release];
//    [_scrollView release];
    [super dealloc];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"QQ20150404-2.png"]];
    
    self.tabBarController.tabBar.hidden = NO;
    //隐藏navigationBar
    self.navigationController.navigationBarHidden = YES;
    //取消自动调整视图
    self.automaticallyAdjustsScrollViewInsets = NO;

    
//    self.navigationController.navigationBar.translucent = YES;
    

    self.tabBarController.tabBar.hidden = NO;
    self.navigationController.navigationBar.translucent = YES;
    //self.tabBarController.tabBar.translucent = YES;

    //页面ScrollView
//    self.scrollView = [[UIScrollView alloc] initWithFrame:self.view.bounds];
    //self.scrollView.bounces = NO;
//    self.scrollView.showsVerticalScrollIndicator = NO;
//    self.scrollView.scrollEnabled = NO;
    
    
    //我的收藏
    MineSnap *collection = [[MineSnap alloc] initWithFrame:CGRectMake(0, self.view.frame.size.height*2/5, self.view.frame.size.width, self.view.frame.size.height/2/5)];
    collection.pic.image = [UIImage imageNamed:@"MineFavo.png"];
    collection.word.frame = CGRectMake(collection.pic.frame.origin.x+collection.pic.frame.size.width+10, 2, 100, collection.frame.size.height-4);
    collection.word.text = @"我的收藏";
    //collection.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"QQ20150403-1.png"]];
    collection.userInteractionEnabled = YES;
    UITapGestureRecognizer *collTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(collTap)];
    [collection addGestureRecognizer:collTap];
    [collTap release];
    //collection.layer.borderColor = [[UIColor grayColor] CGColor];
    //collection.layer.borderWidth = 1;
    
    [self.view addSubview:collection];
    [collection release];
    
    //清除缓存
    MineSnap *cache = [[MineSnap alloc] initWithFrame:CGRectMake(0, collection.frame.size.height+collection.frame.origin.y+10, self.view.frame.size.width, self.view.frame.size.height/2/5)];
    cache.pic.image = [UIImage imageNamed:@"Minecache.png"];
    cache.word.frame = CGRectMake(cache.pic.frame.origin.x+cache.pic.frame.size.width+10, 2, 150, cache.frame.size.height-4);
    cache.word.text = @"清除缓存";
    cache.userInteractionEnabled = YES;
    UITapGestureRecognizer *cacheTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(cacheTap)];
    [cache addGestureRecognizer:cacheTap];
    [cacheTap release];
    //cache.backgroundColor = [UIColor blueColor];
    //cache.layer.borderColor = [[UIColor grayColor] CGColor];
    //cache.layer.borderWidth = 1;
    [self.view addSubview:cache];
    [cache release];
    
    //关于我们
    MineSnap *us = [[MineSnap alloc] initWithFrame:CGRectMake(0, cache.frame.size.height+cache.frame.origin.y+10, self.view.frame.size.width, self.view.frame.size.height/2/5)];
    us.pic.image = [UIImage imageNamed:@"Mineus.png"];
    us.word.frame = CGRectMake(us.pic.frame.origin.x+us.pic.frame.size.width+10, 2, 200, us.frame.size.height-4);
    us.word.text = @"关于我们";
    us.userInteractionEnabled = YES;
    UITapGestureRecognizer *usTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(usTap)];
    [us addGestureRecognizer:usTap];
    [usTap release];
    //us.backgroundColor = [UIColor purpleColor];
//    us.layer.borderColor = [[UIColor grayColor] CGColor];
//    us.layer.borderWidth = 1;
    [self.view addSubview:us];
    [us release];
    
    //意见反馈
    MineSnap *opinion = [[MineSnap alloc] initWithFrame:CGRectMake(0, us.frame.size.height+us.frame.origin.y+10, self.view.frame.size.width, self.view.frame.size.height/2/5)];
    opinion.pic.image = [UIImage imageNamed:@"Mineopinion.png"];
    opinion.word.frame = CGRectMake(opinion.pic.frame.origin.x+opinion.pic.frame.size.width+10, 2, 250, opinion.frame.size.height-4);
    opinion.word.text = @"意见反馈";
    //opinion.backgroundColor = [UIColor cyanColor];
    opinion.userInteractionEnabled = YES;
    UITapGestureRecognizer *opinTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(opinTap)];
    [opinion addGestureRecognizer:opinTap];
    [opinTap release];
//    opinion.layer.borderWidth = 1;
//    opinion.layer.borderColor = [[UIColor grayColor] CGColor];
    [self.view addSubview:opinion];
    [opinion release];

    
//    self.scrollView.contentSize = CGSizeMake(self.view.frame.size.width, opinion.frame.origin.y+opinion.frame.size.height+110);
//    self.scrollView.delegate = self;
//    [self.view addSubview:self.scrollView];
//    [self.scrollView release];
    
}



//我的收藏
- (void)collTap
{
    
    MineFavorTableViewController *mineFavo = [[MineFavorTableViewController alloc] init];
    //self.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:mineFavo animated:NO];
    [mineFavo release];
    
}
//清除缓存
- (void)cacheTap
{
    dispatch_async(
                   dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0)
                   , ^{
                       NSString *cachPath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory,NSUserDomainMask, YES) objectAtIndex:0];
                       
                       NSArray *files = [[NSFileManager defaultManager] subpathsAtPath:cachPath];
                       NSLog(@"files :%ld",[files count]);
                       self.fileCount = [NSString stringWithFormat:@"%ld", [files count]];
                       for (NSString *p in files) {
                           NSError *error;
                           NSString *path = [cachPath stringByAppendingPathComponent:p];
                           if ([[NSFileManager defaultManager] fileExistsAtPath:path]) {
                               [[NSFileManager defaultManager] removeItemAtPath:path error:&error];
                           }
                       }
                       
                       [self performSelectorOnMainThread:@selector(clearCacheSuccess) withObject:nil waitUntilDone:YES];});
    
}
//清除缓存成功
-(void)clearCacheSuccess
{
    //NSLog(@"清理成功");
    NSString *str = [NSString stringWithFormat:@"清理成功,共清理了%@个文件", self.fileCount];
    UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"清理缓存" message:str delegate:self cancelButtonTitle:@"确定" otherButtonTitles: nil];
    alertView.alertViewStyle = UIAlertViewStyleDefault;
    [alertView show];
    [self.view addSubview:alertView];
    [alertView release];
}
//关于我们
- (void)usTap
{
    //NSLog(@"关于我们");
    self.alertView = [[UIAlertView alloc] initWithTitle:@"本版本1.0,感谢使用" message:@"开发组:郭玉勋.章历杰.王润宝" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil];
    self.alertView.alertViewStyle = UIAlertViewStyleDefault;
    [self.alertView show];
    [self.view addSubview:self.alertView];
    [_alertView release];
    NSLog(@"cancelButtonIndex:%ld",self.alertView.cancelButtonIndex);
    self.alertView.transform = CGAffineTransformRotate(self.alertView.transform, M_PI);
    
}
//视图将出现
- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    self.navigationController.navigationBar.hidden = YES;
//    self.tabBarController.tabBar.hidden = NO;
    //self.navigationController.navigationBar.translucent = NO;
    self.navigationController.navigationBar.barTintColor = [UIColor colorFromHexCode:@"FFFFCC"];

}

//意见反馈
- (void)opinTap
{
    OponionViewController *oponion = [[OponionViewController alloc] init];
    [self.navigationController pushViewController:oponion animated:YES];
    [oponion release];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
